<%@ page import="cn.com.betasoft.dsat.web.ap.controllers.AssignRulesController; cn.com.betasoft.dsat.meta.ManagedClass" %>
<% layoutFor("title", {
        out.print guiContext.title;
}) %>
<script src="/javascripts/module/gui-list.js" type="text/javascript" charset="utf-8"></script>
<script src="/javascripts/module/assign_rule.js" type="text/javascript" charset="utf-8"></script>
<style>
    .x-frame-content-right-left {
        display:none;
    }
</style>
<script type="text/javascript">
    $(document).ready(function() {
        changeData("<%~ assignRule.filterMap["expression/Domain"] %>","Domain");
        changeData("<%~ assignRule.filterMap["expression/IP"] %>","IP");
    });
    function goEdit(){
        $.ajax({
                   type: "GET",
                   timeout: 600000,
                   url: "/assign_rules/edit/"+encodeURI("<%~ assignRule.getRuleKey() %>"),
                   cache: false,
                   complete: function(response) {
                       if(response.status == 500 ||response.status == 400 ) {
                            miniOverLay("<%~ l("mos.text.bugInfo") %>");
                            var msg = "<br/>" + response.responseText.toString();
                            $("body").data("dialog").append(msg);
                            $("body").data("dialog").addButton({text:'<%~l("dialog.ok")%>'});
                            $("body").data("dialog").addButton({text:'<%~l("dialog.close")%>'});
                           if (waiting) {
                               $("#task-startTip").remove();
                           }
                       } else if(response.status == 200) {
                            location.href="/assign_rules/edit/"+encodeURI("<%~ assignRule.getRuleKey() %>");
                        }
                   }
        });
    }

    function submitForm() {
        overLay("<%~ l("mos.text.bugInfo") %>");
        ajaxOverLay("/assign_rules/add/assign_rules", "POST", $("form[name='AssignRule']").serializeArray(), 1, "", [],
            function(response){
                if(response.status == 500) {
                    $("body").data("dialog").addButton({text:'<%~l("dialog.ok")%>'});
                } else if(response.status == 200){
                    $("body").data("dialog").addButton({text:'<%~l("dialog.ok")%>',handler:function(){
                        location.href = '/assign_rules'
                    }});
                }
                $("body").data("dialog").addButton({text:'<%~l("dialog.close")%>'});
        });
    }
</script>
<h4 style="text-align:left;"><%~ guiContext.title %></h4><br/>

<%layoutFor("operate_status"){ null }; %>
<% def endpoint_field = {fd ->
     def endPointKey = "endPoint/"+fd.getName();
     def expressionKey= "expression/"+fd.getName();
     def indexKey="index/"+fd.getName();
     def maxKey= "max/"+fd.getName(); %>
    <div class="columns x-gui-field" align="center">
        <table cellspacing="0" cellpadding="0" class="x-gui-field-wrap" align="center">
            <tbody><tr>
                <td class="x-gui-field-label" style="float:left;">
                    <div>
                        <label for="endPoint/<%~ fd.getName() %>"><%~ fd.getLabel() %>:</label>

                    </div>
                </td>
                <td class="x-gui-field-body" >
                    <label align="left" id="expression/<%~ fd.getName() %>" name="expression/<%~ fd.getName() %>">
                       <%~ assignRule.filterMap.get(expressionKey) %>
                    </label>
                    <label align="left" id="endPoint/<%~ fd.getName() %>" name="endPoint/<%~ fd.getName() %>">
                    <%~ assignRule.filterMap.get(endPointKey) %>
                    </label>
                    <label id="and/<%~ fd.getName() %>" name="and/<%~ fd.getName() %>" hidden="false">and</label>
                    <label align="left" id="max/<%~ fd.getName() %>" name="max/<%~ fd.getName() %>" hidden="false">
                     <%~ assignRule.filterMap.get(maxKey) %>
                    </label>
                </td>
            </tr>
        </tbody></table>
    </div>
<script type="text/javascript">
    $(document).ready(function() {
        changeData("<%~ assignRule.filterMap[expressionKey] %>","<%~ fd.getName() %>");
    });
</script>
<% } %>

<% def bottom_button = { %>
    <input type="hidden" name="ruleKey" value="<%~ assignRule.getRuleKey() %>" align="center">
    <a id="rule_modify" class="ui-button" onclick="goEdit();"><div class="ui-button-left">&nbsp;</div><div class="ui-button-center"><%~ l("assign_rule.edit")%></div><div class="ui-button-right">&nbsp;</div></a>
    <a id="rule_back" class="ui-button" onclick="history.go(-1);"><div class="ui-button-left">&nbsp;</div><div class="ui-button-center"><%~ l("assign_rule.back")%></div><div class="ui-button-right">&nbsp;</div></a>
<% } %>

<% layoutFor("operate_status") {}; %>
<%~ formFor(assignRule,[action:'/assign_rules/?_method=PUT',method:'POST']) {form ->
    out.print form.field("id") {field ->
        field.label(l("assign_rule.text.assign_rule_id"));
        String labels="";
        if(assignRule!=null)  labels= assignRule.isNew()?"":assignRule.getId();
        field.editor({
            return "<label name='ruleId' align='center'>"+
                        labels +
                    "</label>";
        })
    }

    out.print form.field("name") {field ->
        field.label(l("assign_rule.name"));
        field.editor({
        return "<label name='assignrule_name' align='center'>"+
                        assignRule.getName() +
               "</label>";
        })
    }

    out.print form.field("engine/key") {field ->
         field.label(l("assign_rule.engine_key"));
        field.editor({
        return "<label name='engine' align='center'>"+
                        assignRule.engine.getName() +
               "</label>";
        })
    }
    out.print form.field("description",{field ->
       field.label(l("assign_rule.description"));
       field.editor({
        return "<label name='description' align='center'>"+
                        assignRule.getDescription()==null?"":assignRule.getDescription() +
               "</label>";
        })
     })

         out.print form.group("virtual",[virtual:true]) { group ->
              group.legend(l("assign_rule.filter"));
         };
            out.print form.field("moType") {field ->
        field.label(l("assign_rule.moType"));
        String labels="";
        if(assignRule.getMoType().equals("All")) labels= l("assign_rule.assignRule.moType.default_value");
        else labels=assignRule.getMoType();
        field.editor({
            return "<label name='moType' align='center'>"+
                        labels +
                    "</label>";
        })
    }

    if(!assignRule.forAll){
         AssignRulesController.
                 findConditionDescriptors(assignRule.moType,assignRule.getFilter()).
                 each(endpoint_field);
    }
    out.print "<br/>"
    out.print "<div class='ui-seg-title'>"+l("assign_rule.mo")+"</div>"
    out.print listFor(mos, {guiList ->
         guiList.cell("mo/displayName", {column, value, record ->
             String href = "/mos/"+record.mo.id;
             String labela = String.format("<a href='%s'>%s</a>", href, value);
             column.formatContent(column.getTemplate("Cell"), [cls:column.getName(),text:labela]);
         })
     });

    out.print bottom_button;
} %>

